package com.xiyuan.smartutils.template.engine.statement;

import com.xiyuan.smartutils.Validates;
import com.xiyuan.smartutils.template.engine.StatementParser;
import com.xiyuan.smartutils.template.engine.TagType;
import com.xiyuan.smartutils.template.engine.VariableMap;
import com.xiyuan.smartutils.template.engine.exception.StatementException;

import java.io.PrintWriter;

/**
 * 块标签
 *
 * @author xiyuan-lgz 2025-02-22 @version v1.0.0 新建与整理
 */
public class _BLOCK extends Statement {
    
    public _BLOCK(StatementParser parser) {
        super(parser);
    }
    
    protected void buildInternal(VariableMap variableMap, PrintWriter out) {}
    
    
    @Override
    protected final void build(VariableMap variableMap, PrintWriter out) {
        if (childs != null && !childs.isEmpty()) {
            variableMap.newVariableSpace();// 创建局部变量空间
            try {
                for (Statement elem : childs) {
                    if (elem instanceof _BLOCK) {
                        ((_BLOCK) elem).buildInternal(variableMap, out);
                    }
                    else {
                        elem.build(variableMap, out);
                    }
                }
            }
            finally {
                variableMap.releaseVariableSpace();
            }
        }
        
    }
    
    @Override
    public TagType getType() {
        return TagType.BLOCK;
    }
    
    @Override
    public boolean parseTag(StatementParser parser) throws StatementException {
        StringBuilder value = new StringBuilder();
        char ch = 0, quote = 0, prvChar = 0;
        int expression_begin_line = 0, expression_begin_column = 0;
        while (parser.hasNext()) {
            prvChar = value.length() == 0 ? 0 : ch;
            ch = parser.next();
            if (Validates.isWhitespace(ch)) {
                continue;
            }
            
            value.append(ch);
            
            if (value.length() == 1) {
                // 记录表达式开始行列
                expression_begin_line = parser.getLineNum();
                expression_begin_column = parser.getColumn();
            }
            if (quote == 0 && (ch == _QUOTE_ || ch == _DOUBLE_QUOTE_)) {
                // 字符串开始
                quote = ch;
                continue;
            }
            
            if (quote != 0 && ch == quote && prvChar == _BACK_SLASH_) {
                // 字符串结束
                quote = 0;
                continue;
            }
            
            if (quote == 0 && ch == _GATHER_) {
                // 标签定义完成
                this.setCompleteTag();
                
                // 标签定义完成
                value.setLength(value.length() - 1);
                this.setStatement(this.getStatement() + value + _GATHER_);
                // 解析表达式
                break;
            }
        }
        
        if (!this.isCompleteTag()) {
            throw new StatementException(this.getPath(),
                                         this.getLineNo(),
                                         this.getBeginColumn(),
                                         "文档已结束。" + getType() + "标签定义缺少‘>’");
        }
        
        return this.isCompleteTag();
    }
}
